package org.dllwh.milvus.v1;

import cn.hutool.core.util.StrUtil;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.QueryResults;
import io.milvus.param.R;
import io.milvus.param.dml.QueryParam;
import io.milvus.response.QueryResultsWrapper;

import java.util.List;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName MilvusQueryHelper
 * @Description Milvus V1 向量搜索操作
 * @Author: <a href="mailto:duleilewuhen@sina.com">独泪了无痕</a>
 * @Date 2025-05-24 14:39
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class MilvusQueryHelper {
    /**
     * @param collectName 指定集合名
     * @param outFields
     * @param expr
     */
    public static void query(String collectName, List<String> outFields, String expr) {
        MilvusServiceClient milvusClient = MilvusConnectHelper.getMilvusServiceClient();
        QueryParam.Builder queryParam = QueryParam.newBuilder();
        queryParam.withCollectionName(collectName);
        if (StrUtil.isNotBlank(expr)) {
            queryParam.withExpr(expr);
        }
        queryParam.withLimit(10L);
        queryParam.withOutFields(outFields);

        R<QueryResults> queryResult = milvusClient.query(queryParam.build());
        Integer status = queryResult.getStatus();
        if (status != 0) {
            System.out.println(("查询失败，cause " + queryResult.getException().getMessage()));
        } else {
            QueryResults queryResults = queryResult.getData();
            QueryResultsWrapper queryResultsWrapper = new QueryResultsWrapper(queryResults);
            List<QueryResultsWrapper.RowRecord> rowRecords = queryResultsWrapper.getRowRecords();
            rowRecords.forEach(System.out::println);
        }
    }
}
